package com.kpx.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.kpx.app.R;
import com.kpx.app.adapter.MyPagerAdapter;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.bean.OrderStatusMenuBean;
import com.kpx.app.configs.BroadcastFilters;
import com.kpx.app.view.SlidingTabLayout;
import com.kpx.app.view.TitleView;
import com.kpx.app.view.ViewPriseShow;
import com.kpx.app.view.ViewPriseShow;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by congdy on 17/2/28.
 */

public class ActivityPriseListNew extends ActivityBase {
    @BindView(R.id.sliding_tabs)
    SlidingTabLayout slidingTabs;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.titleview)
    TitleView titleview;

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_order_list);
        ButterKnife.bind(this);
        initView();
        //网络获取数据-----------------------------
        getMenu();
    }

    @Override
    public void initData() {

    }

    @Override
    public void onReceiver(Context context, Intent intent) {
        if(intent.getAction().equals(BroadcastFilters.BROADCAST_ACTION_REFRESH_DATA)){
            reflushCurrentData();
        }
    }

    /**
     * 菜单列表
     */
    private ArrayList<OrderStatusMenuBean> menuBeas = new ArrayList<>();
    /**
     * 订单页面布局集合
     */
    private ArrayList<View> showViews = new ArrayList<>();

    /**
     * 实例化view
     */
    private void initView() {
        titleview.setTitle("我的评价");
        titleview.setTitleColor(getResources().getColor(R.color.white));
        titleview.setBackFinish();
        slidingTabs.setDistributeEvenly(true);
        slidingTabs.setFillViewport(true);
        slidingTabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
            @Override
            public int getIndicatorColor(int position) {
                return getResources().getColor(R.color.red_FF2048);
            }
        });
    }

    /**
     * 获取菜单和订单列表
     */
    private void getMenu() {
        ArrayList<String> titles = new ArrayList<>();
        String[] orderstatus = {"给商家的评价","来自商家的评价"};
        String[] orderstatustype = {"1", "2"};
        for (int i = 0; i < orderstatus.length; i++) {
            OrderStatusMenuBean b = new OrderStatusMenuBean();
            b.setTitle(orderstatus[i]);
            b.setStatus(Integer.valueOf(orderstatustype[i]));
            titles.add(b.getTitle());
            menuBeas.add(b);
        }
        for (int i = 0; i < menuBeas.size(); i++) {
            ViewPriseShow showView = new ViewPriseShow(this, menuBeas.get(i));
            showViews.add(showView);
        }
        viewpager.setAdapter(new MyPagerAdapter(showViews, titles));
        viewpager.addOnPageChangeListener(new MyOnPageChangeListener());// 设置viewpager的监听事件
        slidingTabs.setViewPager(viewpager);
    }

    /**
     * 显示选择的订单
     */
    public void showStatus(int position) {
        slidingTabs.setCurrent(position);
    }

    /**
     * 获取当前选中页面的位置
     *
     * @return
     */
    public int getSelectedPosition() {
        return slidingTabs.getCurrentPosition();
    }

    /**
     * 刷新当前订单列表
     */
    public void reflushCurrentData() {
        ((ViewPriseShow) showViews.get(viewpager.getCurrentItem())).goReflush();
    }

    @Override
    protected void onResume() {
        super.onResume();
        ((ViewPriseShow) showViews.get(viewpager.getCurrentItem())).goReflush();
    }

    /**
     * 刷新单条订单状态
     *
     * @param intent
     */
    public void reflushCurrentOrder(Intent intent) {
        if (menuBeas.get(viewpager.getCurrentItem()).getStatus() == -1) {
            ((ViewPriseShow) showViews.get(viewpager.getCurrentItem())).reflushOrder(intent);
        } else {
            reflushCurrentData();
        }
    }

    /**
     * 用于监听页面的滑动
     *
     * @author chencong
     * @Description TODO
     * @date 2015年1月10日
     * @Copyright: Copyright (c) 2015 Shenzhen Tentinet Technology Co., Ltd. Inc. All rights reserved.
     */
    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageSelected(int arg0) {
            ((ViewPriseShow) showViews.get(arg0)).refreshData();
        }


        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }

}
